<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/MY_Model.php');

class Roles_m extends MY_Model { 
    function __construct($db = null, $use_slave = null) {
        parent::__construct($db, $use_slave);
    }

    function getRole($params) {

    	if(isset($params['id'])) {
    		$this->db->where('id', $params['id']);
    	}
    	if (isset($params['name'])){
    		$this->db->where('name', $params['name']);
    	}

    	$this->db->select('*', FALSE);
    	$this->db->limit(1);
    	$query = $this->db->get('admin_acl_roles');

    	return $query->row();
    }

	function getRoles($params = array())
    {
    	if(isset($params['id'])) $this->db->where('id', $params['id']);
    	if(isset($params['name'])) $this->db->where('name', $params['name']);
    	if(isset($params['order'])) $this->db->order_by($params['order']);

    	$this->db->select('*');
    	$query = $this->db->get('admin_acl_roles');

    	return $query->result();
    }

    function getOtherRoles($role = ''){
    	$childrenRoles = array();

    	if ($role) {
	    	$childrenRoles[] = $role;
	    	$this->getChildrenRoles($role, $childrenRoles);
    	}

    	$this->db->select('*');

    	if (!empty($childrenRoles))
    		$this->db->where('name not in (\''.implode('\', \'', $childrenRoles).'\')');
    	$query = $this->db->get('admin_acl_roles');

    	return $query->result();
    }

    function getChildrenRoles($role = '', &$childrenRoles) {
    	if(!$role)
    		return TRUE;
    	$this->db->select('name');
    	$this->db->from('admin_acl_roles');
    	$this->db->where('parent_id',  $role);
    	$query = $this->db->get();
    	foreach($query->result() as $row) {
    		$childrenRoles[] = $row->name;
    		$this->getChildrenRoles($row->name, $childrenRoles);
    	}
    	return TRUE;
    }

	function newRole($input = array())
    {
		$row = array(
			'name'			=> $input['name']
			, 'description'	=> $input['description']
			, 'parent_id'	=> $input['parent_id']
		);
        $this->db->insert('admin_acl_roles', $row);
		return $this->db->insert_id();

	}

	function updateRole($id, $input = array()) {
		$data = array();
		if (isset($input['name']))
			$data['name'] 			= $input['name'];
		$data['description'] 	= $input['description'];
		$data['parent_id'] 		= $input['parent_id'];

		return $this->db->update('admin_acl_roles', $data, array('id' => $id));
	}

	function deleteRole($id) {
		$role = $this->getRole(array('id'=>$id));
		if (!$role)
			return false;
		$this->db->update('admin_acl_roles', array('parent_id'=>'guest'), array('parent_id'=>$role->name));
		$this->db->delete('admin_acl_permissions', array('role'=>$role->name));
		$this->db->delete('admin_acl_roles', array('id'=>$id));
		return $this->db->affected_rows();
	}

	function countRoles($params = array())
    {
		return $this->db->count_all_results('admin_acl_roles');
    }

    /*
     * Get all active users
     */
    function getUsers($role_name = '', $filter_status = '0'){
    	$this->db->select('*');

    	if($role_name) {
    		if ($filter_status == 1){
    			$this->db->where('role', $role_name);
    		} else if ($filter_status == 2) {
    			$this->db->where('role != ', $role_name);
    		}
    	}
    	$this->db->where('id != ', $this->session->userdata('user_id'));
    	$this->db->where('is_active', 1);
    	$query = $this->db->get('users');

    	return $query->result();
    }

    /*
     * Update user role
     */
    function updateUserRole($id_array = array(), $role_name = ''){
    	$data = array();

    	if (!$id_array)
    		$id_array[0] = 0;

    	// update users belong to role
    	$data['role'] 	= $role_name;
		$this->db->where('id in ('.implode(",", $id_array).')');
		$this->db->where('role != ', $role_name);
		$this->db->update('users', $data);

		// update users do not belong to role
		$data['role'] = '';
		$this->db->where('id not in ('.implode(",", $id_array).')');
		$this->db->where('role', $role_name);
		$this->db->update('users', $data);
	}
}

?>